#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    writeData();
    readData();


    writeText();
    readText();

    qBuffer();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_ReadFile_clicked()
{
    QString readFileName = QFileDialog::getOpenFileName(
                this
                ,tr("Open Text")
                ,"."
                ,tr("Text(*.txt);;all(*.*)"));

    if (readFileName.isEmpty()) {
        return;
    }

    //文件属性
    QFileInfo qFileInfo(readFileName);
    qDebug()<<"文件大小:"<<qFileInfo.size();
    qDebug()<<"目录对象:"<<qFileInfo.absoluteDir();
    qDebug()<<"文件绝对路径:"<<qFileInfo.absoluteFilePath();
    qDebug()<<"目录绝对路径:"<<qFileInfo.absolutePath();
    qDebug()<<"基础名称:"<<qFileInfo.baseName();
    qDebug()<<"创建时间:"<<qFileInfo.created().toString("yyyy-MM-dd HH:mm:ss");
    qDebug()<<"文件名称:"<<qFileInfo.fileName();
    qDebug()<<"是否可写:"<<qFileInfo.isWritable();
    qDebug()<<"文件后缀:"<<qFileInfo.suffix();
    qDebug()<<"是否为目录:"<<qFileInfo.isDir();
    qDebug()<<"是否为文件:"<<qFileInfo.isFile();


    QFile qFile(readFileName);
    bool isOpenSuccess =   qFile.open(QIODevice::ReadOnly);

    if (!isOpenSuccess) {
        qDebug()<<"打开文件"<<readFileName<<"失败!";
    }


#define READ_SIZE 1024
    //a).以缓冲的方式读取
    QString qString;
    QByteArray qByteArray;

    QByteArray tmp;
    while (!(tmp= qFile.read(READ_SIZE)).isEmpty()) {
        qByteArray+=tmp;
    }
    //1.按照UTF-8编码生成字符串
    //   qString = QString::fromUtf8(qByteArray);
    //2.按照操作系统默认编码
    //   qString = QString::fromLocal8Bit(qByteArray);
    //3.按照UTF-16编码生成字符串
    QTextCodec* qTextCodec = QTextCodec::codecForName("utf-16");
    qString = qTextCodec->toUnicode(qByteArray);
    ui->textEdit->setText(qString);

    //b).一次读取全部文本内容
    //    QByteArray qByteArrayAll = qFile.readAll();
    //    ui->textEdit->setText(qByteArrayAll);
    //QTextCodec::codecForUtfText()


    //c).按行读取文本内容
    //    QByteArray qByteArrayAppend;
    //    while (!qFile.atEnd()) {
    //        qByteArrayAppend+=qFile.readLine();
    //    }
    //    ui->textEdit->setText(qByteArrayAppend);
    qFile.close();
}

void MainWindow::on_WriteFile_clicked()
{
    QString saveFileName = QFileDialog::getSaveFileName(
                this,
                tr("Save File"),
                "./",
                tr("Images (*.png *.xpm *.jpg);;"
                   "Text(*.txt);;"
                   "Execute(*.exe *.dll *.sys);;"
                   "all(*.*)"));


    if (saveFileName.isEmpty()) {
        return;
    }

    QFile qFile;
    qFile.setFileName(saveFileName);
    bool isOpenSuccess =   qFile.open(QIODevice::WriteOnly);

    if (!isOpenSuccess) {
        qDebug()<<"打开文件"<<saveFileName<<"失败!";
    }

    //从控件中读取要保存的文本
    QString textString = ui->textEdit->toPlainText();

    //    qFile.write(textString.toStdString().data());
    //以UTF-8 编码写出数据
    //    qFile.write(textString.toUtf8());
    //以操作系统本地编码写出数据ANSI
    //qFile.write(textString.toLocal8Bit());
    //以UTF-16编码写出数据
    QTextCodec* qTextCodec = QTextCodec::codecForName("utf-16");
    //    release版本不支持此方法
    //    qFile.write(qTextCodec->fromUnicode(textString.unicode()));
    qFile.write(qTextCodec->fromUnicode(textString));
    qFile.close();
}
void MainWindow::readData()
{
    QFile qFile("./abc.txt");
    int isOpenSuccess = qFile.open(QIODevice::ReadOnly);
    if (isOpenSuccess)
    {
        QDataStream qDataStream(&qFile);
        QString str;
        int var;
        qDataStream >> str >> var;
        cout << str;
        cout << var;

        qFile.close();
    }

}
void MainWindow::writeData()
{
    QFile qFile("./abc.txt");
    int isOpenSuccess = qFile.open(QIODevice::WriteOnly);
    if (isOpenSuccess)
    {
        QDataStream qDataStream(&qFile);
        qDataStream <<QString("你好！我是张三；") << 105;
        qFile.close();
    }

}

void MainWindow::readText()
{
    QFile qFile("./text.txt");
    int isOpenSuccess = qFile.open(QIODevice::ReadOnly);
    if (isOpenSuccess)
    {
        QTextStream qTextStream(&qFile);
        qTextStream.setCodec("utf-8");
        //qTextStream.setCodec("utf-16");
        QString str = qTextStream.readAll();
        cout << str;
        qFile.close();
    }
}
void MainWindow::writeText()
{
    QFile qFile("./text.txt");
    int isOpenSuccess = qFile.open(QIODevice::WriteOnly);
    if (isOpenSuccess)
    {
        QTextStream qTextStream(&qFile);
        //qTextStream.setCodec("utf-16");
        qTextStream.setCodec("utf-8");
        qTextStream <<QString("你好！我是张三；QTextStream") << 105;
        qFile.close();
    }
}
void MainWindow::qBuffer()
{
    QBuffer qBuffer;
    qBuffer.open(QIODevice::WriteOnly);
    qBuffer.write("你哈珀");
    qBuffer.write("330");

    cout<<QString(qBuffer.buffer());
    qBuffer.close();
}
